package org.refresh.tests;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.refresh.util.ParsingUtil;

import junit.framework.TestCase;

/******************************************************************************
 * Copyright (c) 2005, 2006 Jules White.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Jules White - initial API and implementation 
 ****************************************************************************/
public class ParsingUtilTest extends TestCase {

	public void testParseFlatList(){
		String list = "[a={3},b.[asdf],_;;[]{}]";
		List l = ParsingUtil.parseFlatList(list);
		assertEquals("a={3}", l.get(0));
		assertEquals("b.[asdf]", l.get(1));
		assertEquals("_;;[]{}", l.get(2));
	}
	
	public void testParseList(){
		String list = "[a={b=3},b.[asdf],[a,b,c],_;;[]{},{c=[a,b,c,d]}]";
		List l = ParsingUtil.parseList(list);
		assertEquals("a={b=3}", l.get(0));
		assertEquals("b.[asdf]", l.get(1));
		assertEquals(ArrayList.class, l.get(2).getClass());
		assertEquals(3, ((List)l.get(2)).size());
		assertEquals("_;;[]{}", l.get(3));
		assertEquals(HashMap.class, l.get(4).getClass());
		assertEquals(ArrayList.class, ((Map)l.get(4)).get("c").getClass());
		assertEquals(4, ((List)((Map)l.get(4)).get("c")).size());
	}
	
	public void testParseMap(){
		String mapstr = "{a=b; c=[a;b;,{},[a{},{a=b}]];d={3=1}}";
		Map map = ParsingUtil.parseMap(mapstr);
		assertEquals("b", map.get("a"));
		assertEquals(ArrayList.class, map.get("c").getClass());
		List l = (List)map.get("c");
		assertEquals("a;b;", l.get(0));
		assertEquals(HashMap.class, l.get(1).getClass());
		assertEquals(0, ((HashMap)l.get(1)).size());
		assertEquals(ArrayList.class, l.get(2).getClass());
		assertEquals(2, ((List)l.get(2)).size());
		assertEquals("a{}", ((List)l.get(2)).get(0));
		assertEquals(HashMap.class, ((List)l.get(2)).get(1).getClass());
		assertEquals(1, ((HashMap)((List)l.get(2)).get(1)).size());
		assertEquals("b", ((HashMap)((List)l.get(2)).get(1)).get("a"));
	}
	
	public void testSimpleParseMap(){
		String mapstr = "{a=b; c=[a;b;,{},[a{},{a=b}]];d={3=1}}";
		Map map = ParsingUtil.parseSimpleMap(mapstr);
		assertEquals("b", map.get("a"));
		assertEquals("[a;b;,{},[a{},{a=b}]]", map.get("c"));
		assertEquals("{3=1}", map.get("d"));
	}
}
